<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="年级" prop="year">-->
<!--        <el-select v-model="queryParams.year" clearable placeholder="请选择" size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.year_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in dict.type.classname_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input type="text" v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="title" />
      <el-table-column label="学号" align="center" prop="year" />
      <el-table-column label="班级" align="center" prop="type" />
      <el-table-column label="照片数量" align="center" prop="type" />
      <el-table-column label="发布时间" align="center" prop="type" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.readTime == null"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >管理</el-button>
          <el-button v-if="scope.row.readTime == null"
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
          >删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 管理对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAddphoto"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-circle-close"
            size="mini"
            :disabled="multiple"
            @click="delphoto"
          >删除</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="photoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="50">
          <template slot-scope="scope">
            {{scope.$index + (currentPagePhoto-1)*pageSizePhoto +1}}
          </template>
        </el-table-column>
        <el-table-column label="图片" align="center" prop="degree">
          <template slot-scope="scope">
            <el-image
              style="width: 120px;height: 60px;"
              :src="scope.row.degree"
              lazy
              :preview-src-list="[scope.row.degree]">
            </el-image>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeletePhoto(scope.row)"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdatePhoto(scope.row)"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalPhoto>0"
        :total="totalPhoto"
        :page.sync="currentPagePhoto"
        :limit.sync="pageSizePhoto"
        @pagination="getList"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加照片对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="titlephoto" :visible.sync="openphoto" width="500px" append-to-body>
      <el-form ref="form" :model="formphoto" :rules="rules" label-width="80px">

        <el-form-item label="姓名" prop="deviceName">
          <el-select v-model="formphoto.deviceType" placeholder="请选择姓名" >
            <el-option
              v-for="item in photosort"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传图片" prop="headmage">
          <imageUpload v-model="formphoto.headmage" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormPhoto">确 定</el-button>
        <el-button @click="cancelPhoto">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { addDevice, delDevice, listdevice, updateDevice } from '@/api/base/device'
import { listgrowphoto } from '@/api/base/grow'

export default {
  name: "phonegl",
  dicts: ['year_type','classname_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        deviceName: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        vendorId:[
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        deviceCode:[
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        schoolCode:[
          { required: true, message: "不能为空", trigger: "blur" }
        ],
      },
      photoList:[],//弹窗照片数据
      pageSizePhoto:10,
      currentPagePhoto:1,
      totalPhoto:0,
      photosort:[{label:'照片',value:0},{label:'视频',value:1}],
      titlephoto:'',
      openphoto:false,
      formphoto:{},
      movie:null,//视频
      urlistmovie:[],
      dialogVisiblevideo:false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**上传视频**/
    videohandleChange(file, fileList) {
      //限制条件可以通过file判断，比如视频格式
      console.log(file, "file");
      console.log(fileList, "fileList");
      let formData = new FormData();
      formData.append("file", file.raw);
      //上传接口
      // addupload(formData,2).then(res => {
      //   //拿到返回的路径
      //   let index = res.data.data.lastIndexOf("=");
      //   let path = res.data.data.substring(index + 1);
      //   this.$message.success("上传成功");
      //   //回显接口
      //   getEventpath(path).then(res => {
      //     //拿到视频路径回显
      //     let url = res.request.responseURL;
      //     this.urlistmovie.push(url);
      //   });
      // });
    },
    Visiblemovie(val) {
      this.movie = val;
      this.dialogVisiblevideo = !this.dialogVisiblevideo;
    },
    /** 查询学生列表 */
    async getList() {
      try {
        this.loading = true;
        const data=listgrowphoto(this.queryParams)
        debugger;
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      }catch (e) {
        console.log(e)
      }

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceName: null,
        schoolCode: null,
        vendorId: null,
        deviceType: null,
        deviceCode: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加照片";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form = row;
      this.open = true;
      this.title = "修改照片";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 新增照片按钮操作 */
    handleAddphoto() {
      this.reset();
      this.openphoto = true;
      this.titlephoto = "添加照片";
    },
    //删除照片
    delphoto(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 提交按钮 */
    submitFormPhoto() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addDevice(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 表单重置
    resetphoto() {
      this.formphoto = {

      };
      this.resetForm("formphoto");
    },
    // 取消照片按钮
    cancelPhoto() {
      this.openphoto = false;
      this.resetphoto();
    },
  }
};
</script>
